<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Storage;
use DB;

/**
 * Class RunGameConfig
 *
 * @package App
 * @mixin \Eloquent
 */
class RunGameConfig extends Model
{
    public $timestamps = false;
    protected $table = "run_game_config";


    /**
     * 修改游戏配置
     * @param $config
     * @param $request
     * @return array
     */
    static function Modify($config, $request)
    {
        DB::beginTransaction();

        //如果修改了game_times
        if ($config->game_times !== intval($request->game_times)) {
            $dvalue = $request->game_times - $config->game_times;
            //更新用户游戏次数
            if (!RunGameLogs::where('id', '>=', 0)->increment('times', $dvalue)) {
                DB::rollBack();

                return ['status' => 2, 'message' => '失败'];
            }
        }
        $config->integral_pre = $request->integral_pre;
        $config->price_limit = $request->price_limit;
        $config->items_num = $request->items_num;
        $config->game_times = $request->game_times;
        $config->rule_content = $request->rule_content;
        foreach ([
                     'list_pic',
                     'home_title',
                     'home_pole',
                     'home_role_1',
                     'home_role_2',
                     'home_role_3',
                     'home_bg',
                     'home_start',
                     'home_price',
                     'home_price1',
                     'home_price2',
                     'home_price3',
                     'gaming_box',
                     'gaming_trap',
                     'gaming_bg',
                     'gaming_dashed',
                     'gaming_true',
                     'gaming_true_after',
                     'gaming_false',
                     'gaming_false_after',
                     'rank_bg_all',
                     'rank_bg_self',
                     'answer_box',
                     'answer_back',
                     'answer_true',
                     'answer_success',
                     'rule_bg',
                     'close',
                 ] as $item) {
            if ($request->has($item)) {
                $config->$item = Storage::url($request->$item->store('/', 'oss'));
            }
        }
        if (!$config->save()) {
            DB::rollBack();

            return ['status' => 2, 'message' => '失败'];
        }

        DB::commit();
        return ['status' => 1, 'message' => '成功'];
    }
}
